// teacher.js
const teacherApp = createApp({
    data() {
        return {
            teachers: [],
            loading: false,
            error: null,
            formData: {
                userNo: '',
                name: '',
                hireYear: '',
                collegeCode: '',
                title: ''
            }
        }
    },
    methods: {
        // 加载教师数据
        async loadTeachers() {
            this.loading = true;
            this.error = null;
            try {
                const response = await fetch(window.contextPath+'/teacher/listAll');
                if (response.ok) {
                    this.teachers = await response.json();
                } else {
                    const errorText = await response.text();
                    this.error = '加载教师数据失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            }catch ( error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }finally {
                this.loading = false;
            }
        },
        // 添加教师
        async addTeacher() {
            if (!this.formData.userNo || this.formData.userNo.length !== 10) {
                this.error = '工号必须为10位数字';
                showErrorModal(this.error);
                return;
            }

            try {
                const response = await fetch(window.contextPath+'/teacher/add', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(this.formData)
                });

                if (response.ok) {
                    alert('添加成功')
                    await this.loadTeachers();
                    this.formData = {
                        userNo: '',
                        name: '',
                        hireYear: '',
                        collegeCode: '',
                        title: ''
                    };
                    closeTeacherAddModal();
                } else {
                    const errorText = await response.text();
                    this.error = '添加教师失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 从工号中提取信息
        async extractInfoFromUserNo() {
            const userNo = this.formData.userNo;
            if (userNo && userNo.length === 10) {
                this.formData.hireYear = userNo.substring(0, 2);
                this.formData.collegeCode = userNo.substring(2, 4);
            } else {
                this.formData.enrollmentYear = '';
                this.formData.collegeCode = '';
            }
        },
        // 删除教师
        async deleteTeacher(userNo) {
            try {
                if (!confirm('确定要删除该该教师吗？这将造成不可挽回的后果,例如该老师所授课程也会被删除。')) {
                    return;
                }
                const response = await fetch(window.contextPath+'/teacher/delete?userNo=' + userNo, {
                    method: 'DELETE'
                });
                if (response.ok) {
                    alert('删除成功')
                    await this.loadTeachers();
                } else {
                    const errorText = await response.text();
                    this.error = '删除教师失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 修改教师信息
        async updateTeacher() {
            alert('该功能正在开发中...')
        }
    },
    mounted() {
        window.teacherAppInstance = this;
    }
});
teacherApp.mount('#teacher-content');

// 显示添加教师表单
function showTeacherAddModal() {
    const modal = document.getElementById('teacherAddModal');
    modal.classList.add('show');
    modal.style.display = 'flex';
}

// 关闭添加教师表单
function closeTeacherAddModal() {
    document.getElementById('teacherAddModal').style.display = 'none';
}
